﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Document_Management.刀具
{
    public partial class CuttingTool_llingyongren_search : Form
    {
        static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["庫存管理"].ConnectionString;
        TextBox tx;
        String bumen;
        public CuttingTool_llingyongren_search(TextBox tx, String bumen)
        {
            this.tx = tx;
            this.bumen = bumen;
            InitializeComponent();
        }

        private void CuttingTool_llingyongren_search_Load(object sender, EventArgs e)
        {
            Load_data("sp_刀具領用人_lingyong_load");
        }

        public void Load_data(String proc)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter(proc, con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                da.SelectCommand.Parameters.AddWithValue("@領用部門代號", bumen);
                DataTable dt = new DataTable();
                da.Fill(dt);
                data_lingyongren.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                data_lingyongren.DataSource = dt;
                PublicClass.setRowNumber(data_lingyongren);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        public void search(String sp_search)
        {
            try
            {
                SqlConnection con = new SqlConnection(connectionString);
                con.Open();
                SqlDataAdapter cmd = new SqlDataAdapter(sp_search, con);
                cmd.SelectCommand.CommandType = CommandType.StoredProcedure;
                cmd.SelectCommand.Parameters.AddWithValue("@領用部門代號", bumen);
                if (txt查詢.Text != "") cmd.SelectCommand.Parameters.AddWithValue("@姓名或代號", txt查詢.Text);
                DataTable dt = new DataTable();
                cmd.Fill(dt);
                data_lingyongren.DataSource = dt;
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void bt_確定_Click(object sender, EventArgs e)
        {
            try
            {
                int r = data_lingyongren.CurrentCell.RowIndex;
                string text = data_lingyongren.Rows[r].Cells[0].Value.ToString();
                tx.Text = text;
                this.Close();
            }
            catch
            {
                MessageBox.Show("資料不對");
            }
        }

        private void bt_查詢_Click(object sender, EventArgs e)
        {
            search("sp_刀具領用人_lingyong_search");
        }
    }
}
